import axios from 'axios'
import { store } from '@/store'
import apiRoot from '@/utils/domain'

import { ElMessage } from 'element-plus'
// 创建axios
const instance = axios.create({
  baseURL: apiRoot.BASE_API_URL,
  headers: { 'X-Requested-With': 'XMLHttpRequest', 'Content-Type': 'application/json' },
  // transformRequest: [data => JSON.stringify(data)],
  // withCredentials: true,
  timeout: 20000
})

instance.interceptors.request.use(
  (config: any) => {
    // 上传到阿里云 不需要传递 token和cookie
    if (!config.isOss) {
      // 登录未调好之前，写死的token，登录联调后直接删除就好
      config.headers['sms-auth-token'] = store.getters['common/getSmsToken']
    }

    return config
  },
  (error) => {
    return Promise.reject(error)
  }
)

instance.interceptors.response.use(
  (response: any) => {
    if (response.data.code == 0) {
      return Promise.resolve(response.data instanceof Object ? response.data : JSON.parse(response.data))
    } else {
      if ([500].includes(response.data.code)) {
        if (response.data.msg) {
          ElMessage.error(response.data.msg)
        } else {
          ElMessage.error('啊哦，数据出问题了哟！')
        }
      } else if (response.data.code == 100000) {
        ElMessage.warning(response.data.msg)
      } else {
        ElMessage.error(response.data.msg)
      }

      // 上传阿里云
      if (['/scrm-api/console/media/file/apply-upload-param'].includes(response.config.url)) {
        return Promise.resolve(response)
      }

      return Promise.reject(response.data)
    }
  },
  (error) => {
    if (error.response) {
      ElMessage.error(error.response.statusText)
    } else {
      ElMessage.error('服务器未响应，请稍后重试！')
    }
    return Promise.reject(error.response)
  }
)

/**
 * get 请求方法
 * @param url
 * @param params
 * @returns {Promise}
 */
export function get(url: string, params = {}, option = {}) {
  return new Promise((resolve, reject) => {
    instance
      .get(url, { params, ...option })
      .then((response) => {
        resolve(response.data)
      })
      .catch((err) => {
        reject(err)
      })
  })
}

/**
 * post 请求方法
 * @param url
 * @param data
 * @returns {Promise}
 */
export function post(url: string, data = {}, option = {}) {
  return new Promise((resolve, reject) => {
    instance.post(url, data, { ...option }).then(
      (response) => {
        resolve(response.data)
      },
      (err) => {
        reject(err)
      }
    )
  })
}

/**
 * put 方法封装
 * @param url
 * @param data
 * @returns {Promise}
 */
export function put(url: string, data = {}, option = {}) {
  return new Promise((resolve, reject) => {
    instance.put(url, data, { ...option }).then(
      (response) => {
        resolve(response.data)
      },
      (err) => {
        reject(err)
      }
    )
  })
}

/**
 * delete 方法封装
 * @param url
 * @param data
 * @returns {Promise}
 */
export function del(url: string, data = {}, option = {}) {
  return new Promise((resolve, reject) => {
    instance.delete(url, { data, ...option }).then(
      (response) => {
        resolve(response.data)
      },
      (err) => {
        reject(err)
      }
    )
  })
}

/**
 * all 并发请求封装
 * @param url
 * @param arr
 * @returns {Promise}
 */
// export function all(arr: any) {
//   return new Promise((resolve, reject) => {
//     axios
//       .all(arr)
//       .then(
//         axios.spread(function () {
//           const resultArr = [...arguments]
//           resolve(resultArr)
//         })
//       )
//       .catch((err) => {
//         reject(err)
//       })
//   })
// }

export default { get, post, put, delete: del }
